//
//  AnuseViewController.m
//  Funny
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 King. All rights reserved.
//

#import "AnuseViewController.h"

@interface AnuseViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, assign) NSInteger flag;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation AnuseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.flag = 1;
    [self handleDataPullUp];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[AnuseTableViewCell class] forCellReuseIdentifier:@"reuse"];
        _tableView.header = [MyRefreshHeaderGif headerWithRefreshingTarget:self refreshingAction:@selector(handleDataPullDown)];
        _tableView.footer = [MyRefreshFooterGif footerWithRefreshingTarget:self refreshingAction:@selector(handleDataPullUp)];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 340 * W;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnuseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.model = _dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    MainAnuseViewController *MainAnuseVC = [MainAnuseViewController shareViewController];
    MainAnuseVC.ID = [_dataArr[indexPath.row] placeid];
    [self.navigationController pushViewController:MainAnuseVC animated:NO];
}

// 下拉刷新

- (void)handleDataPullDown
{
    self.flag = 1;
    self.dataArr = NULL;
    [self handelData];
    [self.tableView.header endRefreshing];
}

// 上拉加载

- (void)handleDataPullUp
{
    self.flag += 11;
    [self handelData];
    [self.tableView.footer endRefreshing];
}

// 数据处理
- (void)handelData
{
    NSString *str = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/ps?cityid=%@&type=%@&sort=default&gwtype&_nf=1&from=%ld&num=10",self.key,self.type,((long)self.flag)];
    [NetHandler getDataWithURL:str completion:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *dataDic in arr) {
            AnuseModel *model = [AnuseModel modelWithDic:dataDic];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
